
bt.module({builder: function(bt) {
    var has_captcha = false;
    function check_username(username, hint) {
        if (!username) {
            hint.text('请输入用户名');
            return false;
        }
        if (username.length < 2 || username.length > 30) {
            hint.text('用户名长度应为2~30个字符');
            return false;
        }

        var re = /^[a-z_A-Z0-9一-龥]+$/;
        if (!re.test(username)) {
            hint.text('用户名只能包含中文字符、英文字母、数字和下划线(_)');
            return false;
        }

        return true;
    }

    function check_email(email, hint) {
        if (!email) {
            hint.text('请输入电子邮件');
            return false;
        }

        var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!re.test(email)) {
            hint.text('请输入正确的邮件地址');
            return false;
        }

        return true;
    }

    function check_password(input) {
        var password = $(input).val();
        var hint = $(input).next();
        if (password.length < 6) {
            hint.text('密码至少6位');
            return false;
        } else {
            hint.text('');
            return true;
        }
    }

    function check_password_repeat(input, password) {
        var password_repeat = $(input).val();
        var hint = $(input).next();
        if (password != password_repeat) {
            hint.text('两次输入的密码不一致');
            return false;
        } else {
            hint.text('');
            return true;
        }
    }

    var _changeUsername = function(msg, oauth_version) {
        var name = prompt(msg, "");
        if(name == null) {
            j$(window.location).attr("href", "/accounts/login/");
        } else if(j$.trim(name)) {
            j$.post("/accounts/change_name/",
                {"name": name,
                 "oauth_version": oauth_version
                },
                function(resp, textStatus, jqXHR) {
                    if(!resp.error) {
                        j$(window.location).attr("href", "/");
                    } else {
                        _changeUsername(resp.error);
                    }
                },
                "json"
            );
        } else {
            _changeUsername(msg);
        }
    };

    var changeUsername = function(oauth_version) {
        j$(document).ready(function() {
            _changeUsername("微博用户名已被使用，换一个吧", oauth_version);
        });
    };
 
    $(document).ready(function(){
        $('#captcha-img').on("click", function() {
            $(this).attr("src", "/captcha.jpg?v=" + parseInt(Math.random() * 1000000));
        });
        $('#id_country_num').on('click',function(){
            $('.country-num-select').show();
        });
        $('.country-num-index span').on('click',function(){
            if(!$(this).hasClass('cur')){
                var index = $('.country-num-index span').index($(this));
                $('.country-num-index .cur').removeClass('cur');
                $(this).addClass('cur')
                $('.country-num-list .cur').removeClass('cur');
                $('.country-num-list ul').eq(index).addClass('cur');
            }
        })
        $('.country-item').on('click',function(){
            $('#id_country_num').val($(this).attr("data-num"));
            $('.country-num-select').hide();
        });
        $('.country-num-select').on('mouseleave',function(){
            $('.country-num-select').hide();
        });
        $('#login-form-phone').on('submit',function(e){
            var countryNum = $('#id_country_num').val(),
                mobileNum = $('#id_mobile_num').val(),
                password = $(this).find('#id_password').val();
            if(mobileNum == ""){
                alert("请输入手机号");
                e.preventDefault();
                return;
            }
            if(countryNum == ""){
                alert("请选择国家和地区");
                e.preventDefault();
                return;
            }
            if(!/^[0-9]*$/.test(mobileNum) || (countryNum == '86' && !/^[0-9]{11}$/.test(mobileNum))){
                alert("请输入正确的手机号码");
                e.preventDefault();
                return;
            }
            if(password == ""){
                alert("请输入密码");
                e.preventDefault();
                return;
            }
        });
        $('#id_username').blur(function() {
            var username = $(this).val();
            var hint = $(this).next();
            if (!check_username(username, hint))
                return;

            $.ajax({
                url: '/accounts/check_username/',
                data: {username: username},
                success: function(msg) {
                    var error = eval(msg).error;
                    if (error)
                        hint.text(error);
                    else
                        hint.text('');
                }
            });
        });

        $('#id_email').blur(function() {
            var email = $(this).val();
            var hint = $(this).next();
            if (!check_email(email, hint))
                return;

            $.ajax({
                url: '/accounts/check_email/',
                data: {email: $(this).val()},
                success: function(msg) {
                    var error = eval(msg).error;
                    if (error)
                        hint.text(error);
                    else
                        hint.text('');
                }
            });
        });

        $('#id_password').blur(function() {
            check_password($(this));
        });

        $('#id_password_repeat').blur(function() {
            var password = $('#id_password').val();
            check_password_repeat($(this), password);
        });

        $('form#minimum-signup-form').submit(function() {
            var username = $('#id_username').val();
            var username_hint = $('#id_username').next();
            var email = $('#id_email').val();
            var email_hint = $('#id_email').next();
            var password = $('#id_password').val();
            var _this = this;

            username_ok = check_username(username, username_hint);
            email_ok = check_email(email, email_hint);
            password_ok = check_password($('#id_password'));
            
            if (!email_ok) {
                $('#id_email').focus();
                return false;
            }
            if (!username_ok) {
                $('#id_username').focus();
                return false;
            }
            if (!password_ok) {
                $('#id_password').focus();
                return false;
            }
            $.post($(this).attr("action"),
                   $(this).serialize(),
                   function(resp) {
                       if(resp.status == 0) {
                           location.reload();
                       } else {
                           $(_this).find(".error").first().text(resp.error);
                       }
                   });

            return false;
        });

        $('form#signup').submit(function() {
            var username = $('#id_username').val();
            var username_hint = $('#id_username').next();
            var email = $('#id_email').val();
            var email_hint = $('#id_email').next();
            var password = $('#id_password').val();

            username_ok = check_username(username, username_hint);
            email_ok = check_email(email, email_hint);
            password_ok = check_password($('#id_password'));
            
            if (!email_ok) {
                $('#id_email').focus();
                return false;
            }
            if (!username_ok) {
                $('#id_username').focus();
                return false;
            }
            if (!password_ok) {
                $('#id_password').focus();
                return false;
            }
            return true;
        });

        $('#reset-password-form').submit(function() {
            var password = $('#id_password').val();
            password_ok = check_password($('#id_password'));
            password_repeat_ok = check_password_repeat($('#id_password_repeat'), password);
            
            if (!password_ok) {
                $('#id_password').focus();
                return false;
            }
            if (!password_repeat_ok) {
                $('#id_password_repeat').focus();
                return false;
            }
            return true;
        });

    });

    bt.account.changeUsername = changeUsername;
}});
